#include <stdio.h>
#include <stdlib.h>
#include "test.h"
#include "testTypeSupport.h"
#include "dds_domain_participant_factory.h"
#include "dds_domain_participant.h"
#include "dds_publisher.h"
#include "dds_subscriber.h"
#include "dds_topic.h"
#include "dds_datareader.h"
#include "dds_datawriter.h"
#include "dds_infrastructure.h"
#include <wchar.h>

void fillSimpleStruct(test_SimpleStruct* data, int offset) {
    // 基本数据类型 
    data->enumVal = (test_EnumExample)(0 + offset);
    data->bVal = (1 + offset) % 2;
    data->cVal = 'A' + (2 + offset) % 26;
    data->oVal = (unsigned char)(3 + offset);
    data->int8Val = (char)(4 + offset);
    data->uint8Val = (unsigned char)(5 + offset);
    data->wcVal = (wchar_t)(L'A' + (6 + offset) % 26);
    data->shortVal = (short)(7 + offset);
    data->ushortVal = (unsigned short)(8 + offset);
    data->int16Val = (short)(9 + offset);
    data->uint16Val = (unsigned short)(10 + offset);
    data->longVal = 11 + offset;
    data->ulongVal = 12 + offset;
    data->int32Val = 13 + offset;
    data->uint32Val = 14 + offset;
    data->floatVal = 15.0f + offset;
    data->doubleVal = 16.0 + offset;
    data->ldoubleVal.ld = 17.0 + offset;
    data->llVal = 18 + offset;
    data->ullVal = 19 + offset;
    data->int64Val = 20 + offset;
    data->uint64Val = 21 + offset;

    // 基本数据类型数组
    for (int i = 0; i < 2; ++i) data->enumArrVal[i] = (test_EnumExample)(22 + i + offset);
    for (int i = 0; i < 3; ++i) data->bArrVal[i] = (23 + i + offset) % 2;
    for (int i = 0; i < 4; ++i) data->cArrVal[i] = 'A' + (24 + i + offset) % 26;
    for (int i = 0; i < 5; ++i) data->oArrVal[i] = (unsigned char)(25 + i + offset);
    for (int i = 0; i < 6; ++i) data->int8ArrVal[i] = (char)(26 + i + offset);
    for (int i = 0; i < 7; ++i) data->uint8ArrVal[i] = (unsigned char)(27 + i + offset);
    for (int i = 0; i < 8; ++i) data->wcArrVal[i] = (wchar_t)(L'A' + (28 + i + offset) % 26);
    for (int i = 0; i < 9; ++i) data->shortArrVal[i] = (short)(29 + i + offset);
    for (int i = 0; i < 10; ++i) data->ushortArrVal[i] = (unsigned short)(30 + i + offset);
    for (int i = 0; i < 11; ++i) data->int16ArrVal[i] = (short)(31 + i + offset);
    for (int i = 0; i < 12; ++i) data->uint16ArrVal[i] = (unsigned short)(32 + i + offset);
    for (int i = 0; i < 13; ++i) data->longArrVal[i] = 33 + i + offset;
    for (int i = 0; i < 14; ++i) data->ulongArrVal[i] = 34 + i + offset;
    for (int i = 0; i < 15; ++i) data->int32ArrVal[i] = 35 + i + offset;
    for (int i = 0; i < 16; ++i) data->uint32ArrVal[i] = 36 + i + offset;
    for (int i = 0; i < 17; ++i) data->floatArrVal[i] = (float)(37 + i + offset);
    for (int i = 0; i < 18; ++i) data->doubleArrVal[i] = (double)(38 + i + offset);
    for (int i = 0; i < 19; ++i) data->ldoubleArrVal[i].ld = (long double)(39 + i + offset);
    for (int i = 0; i < 20; ++i) data->llArrVal[i] = 40 + i + offset;
    for (int i = 0; i < 21; ++i) data->ullArrVal[i] = 41 + i + offset;
    for (int i = 0; i < 22; ++i) data->int64ArrVal[i] = 42 + i + offset;
    for (int i = 0; i < 23; ++i) data->uint64ArrVal[i] = 43 + i + offset;

    // 基本数据类型sequence
    DDS_BooleanSequence_resize(&data->bSeq, 2);
    for (int i = 0; i < 2; ++i) DDS_BooleanSequence_data(&data->bSeq)[i] = (24 + i + offset) % 2;

    DDS_CharSequence_resize(&data->cSeq, 3);
    for (int i = 0; i < 3; ++i) DDS_CharSequence_data(&data->cSeq)[i] = 'A' + (25 + i + offset) % 26;

    DDS_UCharSequence_resize(&data->oSeq, 4);
    for (int i = 0; i < 4; ++i) DDS_UCharSequence_data(&data->oSeq)[i] = (unsigned char)(26 + i + offset);

    DDS_CharSequence_resize(&data->int8Seq, 5);
    for (int i = 0; i < 5; ++i) DDS_CharSequence_data(&data->int8Seq)[i] = (char)(27 + i + offset);

    DDS_UCharSequence_resize(&data->uint8Seq, 6);
    for (int i = 0; i < 6; ++i) DDS_UCharSequence_data(&data->uint8Seq)[i] = (unsigned char)(28 + i + offset);

    DDS_WCharSequence_resize(&data->wcSeq, 7);
    for (int i = 0; i < 7; ++i) DDS_WCharSequence_data(&data->wcSeq)[i] = (wchar_t)(L'A' + (29 + i + offset) % 26);

    DDS_ShortSequence_resize(&data->shortSeq, 8);
    for (int i = 0; i < 8; ++i) DDS_ShortSequence_data(&data->shortSeq)[i] = (short)(30 + i + offset);

    DDS_UShortSequence_resize(&data->ushortSeq, 9);
    for (int i = 0; i < 9; ++i) DDS_UShortSequence_data(&data->ushortSeq)[i] = (unsigned short)(31 + i + offset);

    DDS_ShortSequence_resize(&data->int16Seq, 10);
    for (int i = 0; i < 10; ++i) DDS_ShortSequence_data(&data->int16Seq)[i] = (short)(32 + i + offset);

    DDS_UShortSequence_resize(&data->uint16Seq, 11);
    for (int i = 0; i < 11; ++i) DDS_UShortSequence_data(&data->uint16Seq)[i] = (unsigned short)(33 + i + offset);

    DDS_LongSequence_resize(&data->longSeq, 12);
    for (int i = 0; i < 12; ++i) DDS_LongSequence_data(&data->longSeq)[i] = 34 + i + offset;

    DDS_ULongSequence_resize(&data->ulongSeq, 13);
    for (int i = 0; i < 13; ++i) DDS_ULongSequence_data(&data->ulongSeq)[i] = 35 + i + offset;

    DDS_LongSequence_resize(&data->int32Seq, 14);
    for (int i = 0; i < 14; ++i) DDS_LongSequence_data(&data->int32Seq)[i] = 36 + i + offset;

    DDS_ULongSequence_resize(&data->uint32Seq, 15);
    for (int i = 0; i < 15; ++i) DDS_ULongSequence_data(&data->uint32Seq)[i] = 37 + i + offset;

    DDS_FloatSequence_resize(&data->floatSeq, 16);
    for (int i = 0; i < 16; ++i) DDS_FloatSequence_data(&data->floatSeq)[i] = (float)(38 + i + offset);

    DDS_DoubleSequence_resize(&data->doubleSeq, 17);
    for (int i = 0; i < 17; ++i) DDS_DoubleSequence_data(&data->doubleSeq)[i] = (double)(39 + i + offset);

    DDS_LongDoubleSequence_resize(&data->ldoubleSeq, 18);
    for (int i = 0; i < 18; ++i) DDS_LongDoubleSequence_data(&data->ldoubleSeq)[i].ld = (long double)(40 + i + offset);

    DDS_LongLongSequence_resize(&data->llSeq, 19);
    for (int i = 0; i < 19; ++i) DDS_LongLongSequence_data(&data->llSeq)[i] = 41 + i + offset;

    DDS_ULongLongSequence_resize(&data->ullSeq, 20);
    for (int i = 0; i < 20; ++i) DDS_ULongLongSequence_data(&data->ullSeq)[i] = 42 + i + offset;

    DDS_LongLongSequence_resize(&data->int64Seq, 21);
    for (int i = 0; i < 21; ++i) DDS_LongLongSequence_data(&data->int64Seq)[i] = 43 + i + offset;

    DDS_ULongLongSequence_resize(&data->uint64Seq, 22);
    for (int i = 0; i < 22; ++i) DDS_ULongLongSequence_data(&data->uint64Seq)[i] = 44 + i + offset;

    // 字符串
    char tmpValue[64];
    snprintf(tmpValue, sizeof(tmpValue), "String_%d", 45 + offset);
    DDSCString_assign_cstr(&data->bstr, tmpValue);

    wchar_t tmpWValue[64];
    swprintf(tmpWValue, sizeof(tmpWValue) / sizeof(wchar_t), L"WideString_%d", 46 + offset);
    DDSCWString_assign_cstr(&data->bwstr, tmpWValue);

    // map
    for (int i = 0; i < 2; ++i) {
        DDS_Long key = 47 + i + offset;
        DDS_Short value = (short)(47 + i + offset);
        DDS_LongDDS_ShortMap_insert(&data->lsMap, &key, &value);
    }

    for (int i = 0; i < 3; ++i) {
        sprintf(tmpValue, "Key_%d", 48 + i + offset);
        DDS_CString* key = DDSCString_from_cstr(tmpValue);
        DDS_Double value = 48.0 + i + offset;
        DDS_CStringDDS_DoubleMap_insert(&data->ldMap, key, &value);
        DDSCString_destroy(key);
    }

    for (int i = 0; i < 4; ++i) {
        wchar_t tmpWValue[64];
        swprintf(tmpWValue, sizeof(tmpWValue) / sizeof(wchar_t), L"Value_%d", 49 + i + offset);
        DDS_CWString* value = DDSCWString_from_cstr(tmpWValue);
        DDS_Float key = 49.0f + i + offset;
        DDS_FloatDDS_CWStringMap_insert(&data->fwsMap, &key, value);
        DDSCWString_destroy(value);
    }
}

void fillComplexStruct(test_ComplexStruct* data, int offset) {
    // 继承的父类成员
    char tmpValue[64];
    snprintf(tmpValue, sizeof(tmpValue), "ParentName_%d", 0 + offset);
    DDSCString_assign_cstr(&data->parent.name, tmpValue);
    // 填充SimpleStruct成员
    fillSimpleStruct(&data->structMem, 1 + offset);
    // 填充SimpleStruct数组成员
    for (int i = 0; i < 2; ++i) {
        fillSimpleStruct(&data->structArrayMem[i], 2 + i + offset);
    }
    // 填充SimpleStruct序列成员
    test_SimpleStructSequence_resize(&data->structSeqMem, 16);
    for (int i = 0; i < 16; ++i) {
        test_SimpleStruct* curEle = test_SimpleStructSequence_at(&data->structSeqMem, i);
        fillSimpleStruct(curEle, 3 + i + offset);
    }
    // 填充SimpleStruct map成员
    for (int i = 0; i < 3; ++i) {
        char tmpKey[64];
        snprintf(tmpKey, sizeof(tmpKey), "Key_%d", 4 + i + offset);
        DDS_CString* key = DDSCString_from_cstr(tmpKey);
        test_SimpleStruct* value = test_SimpleStruct_create();
        fillSimpleStruct(value, 4 + i + offset);
        DDS_CStringtest_SimpleStructMap_insert(&data->structMapMem, key, value);
        DDSCString_destroy(key);
        test_SimpleStruct_destroy(value);
    }
}

extern int test_ComplexStruct_get_serialized_data_size(test_ComplexStruct* data, unsigned int currentAlignment);
extern int test_ComplexStruct_serialize(test_ComplexStruct* data, char* buffer, unsigned int bufferSize);
extern int test_ComplexStruct_deserialize(test_ComplexStruct* data, const char* buffer, unsigned int bufferSize);

int test_cdr_serialize(char** bufferOut, unsigned int* bufferSizeOut) {
    // Create and fill test_ComplexStruct object
    test_ComplexStruct* data = test_ComplexStruct_create();
    if (data == NULL) {
        printf("Failed to create test_ComplexStruct instance\n");
        return -1;
    }
    fillComplexStruct(data, 0);

    // Get serialized data size
    size_t bufferSize = test_ComplexStruct_get_serialized_data_size(data, 0);
    if (bufferSize == 0) {
        printf("Failed to get serialized data size\n");
        test_ComplexStruct_destroy(data);
        return -1;
    }

    // Allocate buffer for serialization
    char* buffer = (char*)malloc(bufferSize);
    if (buffer == NULL) {
        printf("Failed to allocate memory for serialization buffer\n");
        test_ComplexStruct_destroy(data);
        return -1;
    }

    int ret = test_ComplexStruct_serialize(data, buffer, bufferSize);
    if (ret != 0) {
        printf("Serialization failed\n");
        free(buffer);
        test_ComplexStruct_destroy(data);
        return -1;
    }

    // Write serialized data to file in hexadecimal format
    FILE* outFile = fopen("test_cdr_serialize.txt", "wb");
    if (outFile == NULL) {
        printf("Failed to open file for writing\n");
        free(buffer);
        test_ComplexStruct_destroy(data);
        return -1;
    }

    for (size_t i = 0; i < bufferSize; ++i) {
        // Write 16 bytes per line
        if (i % 16 == 0 && i != 0) {
            fprintf(outFile, "\n");
        }
        fprintf(outFile, "%02x ", (unsigned char)buffer[i]);
    }

    // set output parameters
    if (bufferOut != NULL) {
        *bufferOut = buffer;
    } else {
        free(buffer);
    }

    if (bufferSizeOut != NULL) {
        *bufferSizeOut = bufferSize;
    }

    // Clean up
    fclose(outFile);
    test_ComplexStruct_destroy(data);
    return 0;
}

#define check(name, expected, actual)                                           \
    if (expected != actual) {                                                   \
        printf("Error in %s: expected %s, got %s\n", name, #expected, #actual); \
    }

static void checkWide(const char* name, const wchar_t expected, const wchar_t actual) {
    if (expected != actual) {
        printf("Error in %s: expected ", name);
        wprintf(L"%lc", expected);
        printf(", got ");
        wprintf(L"%lc", actual);
        printf("\n");
    }
}

void checkSimpleStruct(const test_SimpleStruct* data, int offset) {
    // 基本数据类型
    check("enumVal", (test_EnumExample)(0 + offset), data->enumVal);
    check("bVal", (1 + offset) % 2, data->bVal);
    check("cVal", 'A' + (2 + offset) % 26, data->cVal);
    check("oVal", (unsigned char)(3 + offset), data->oVal);
    check("int8Val", (char)(4 + offset), data->int8Val);
    check("uint8Val", (unsigned char)(5 + offset), data->uint8Val);
    check("wcVal", (wchar_t)(L'A' + (6 + offset) % 26), data->wcVal);
    check("shortVal", (short)(7 + offset), data->shortVal);
    check("ushortVal", (unsigned short)(8 + offset), data->ushortVal);
    check("int16Val", (short)(9 + offset), data->int16Val);
    check("uint16Val", (unsigned short)(10 + offset), data->uint16Val);
    check("longVal", 11 + offset, data->longVal);
    check("ulongVal", 12 + offset, data->ulongVal);
    check("int32Val", 13 + offset, data->int32Val);
    check("uint32Val", 14 + offset, data->uint32Val);
    check("floatVal", (15.0f + offset), data->floatVal);
    check("doubleVal", (16.0 + offset), data->doubleVal);
    check("ldoubleVal", (17.0 + offset), data->ldoubleVal.ld);
    check("llVal", 18 + offset, data->llVal);
    check("ullVal", 19 + offset, data->ullVal);
    check("int64Val", 20 + offset, data->int64Val);
    check("uint64Val", 21 + offset, data->uint64Val);

    // 字符串
    char expectedStr[64];
    snprintf(expectedStr, sizeof(expectedStr), "String_%d", 45 + offset);
    if (strcmp(expectedStr, DDSCString_c_str(&data->bstr)) != 0) {
        printf("Error in bstr: expected %s, got %s\n", expectedStr, DDSCString_c_str(&data->bstr));
    }

    wchar_t expectedWStr[64];
    swprintf(expectedWStr, sizeof(expectedWStr) / sizeof(wchar_t), L"WideString_%d", 46 + offset);
    if (wcscmp(expectedWStr, DDSCWString_c_str(&data->bwstr)) != 0) {
        printf("Error in bwstr: expected ");
        wprintf(L"%ls", expectedWStr);
        printf(", got ");
        wprintf(L"%ls", DDSCWString_c_str(&data->bwstr));
        printf("\n");
    }

    // 检查基本数据类型数组
    for (int i = 0; i < 2; ++i) {
        check("enumArrVal", (test_EnumExample)(22 + i + offset), data->enumArrVal[i]);
    }
    for (int i = 0; i < 3; ++i) {
        check("bArrVal", (23 + i + offset) % 2, data->bArrVal[i]);
    }
    for (int i = 0; i < 4; ++i) {
        check("cArrVal", 'A' + (24 + i + offset) % 26, data->cArrVal[i]);
    }
    for (int i = 0; i < 5; ++i) {
        check("oArrVal", (unsigned char)(25 + i + offset), data->oArrVal[i]);
    }
    for (int i = 0; i < 6; ++i) {
        check("int8ArrVal", (char)(26 + i + offset), data->int8ArrVal[i]);
    }
    for (int i = 0; i < 7; ++i) {
        check("uint8ArrVal", (unsigned char)(27 + i + offset), data->uint8ArrVal[i]);
    }
    for (int i = 0; i < 8; ++i) {
        check("wcArrVal", (wchar_t)(L'A' + (28 + i + offset) % 26), data->wcArrVal[i]);
    }
    for (int i = 0; i < 9; ++i) {
        check("shortArrVal", (short)(29 + i + offset), data->shortArrVal[i]);
    }
    for (int i = 0; i < 10; ++i) {
        check("ushortArrVal", (unsigned short)(30 + i + offset), data->ushortArrVal[i]);
    }
    for (int i = 0; i < 11; ++i) {
        check("int16ArrVal", (short)(31 + i + offset), data->int16ArrVal[i]);
    }
    for (int i = 0; i < 12; ++i) {
        check("uint16ArrVal", (unsigned short)(32 + i + offset), data->uint16ArrVal[i]);
    }
    for (int i = 0; i < 13; ++i) {
        check("longArrVal", 33 + i + offset, data->longArrVal[i]);
    }
    for (int i = 0; i < 14; ++i) {
        check("ulongArrVal", 34 + i + offset, data->ulongArrVal[i]);
    }
    for (int i = 0; i < 15; ++i) {
        check("int32ArrVal", 35 + i + offset, data->int32ArrVal[i]);
    }
    for (int i = 0; i < 16; ++i) {
        check("uint32ArrVal", 36 + i + offset, data->uint32ArrVal[i]);
    }
    for (int i = 0; i < 17; ++i) {
        check("floatArrVal", (37 + i + offset), data->floatArrVal[i]);
    }
    for (int i = 0; i < 18; ++i) {
        check("doubleArrVal", (38 + i + offset), data->doubleArrVal[i]);
    }
    for (int i = 0; i < 19; ++i) {
        check("ldoubleArrVal", (39 + i + offset), data->ldoubleArrVal[i].ld);
    }
    for (int i = 0; i < 20; ++i) {
        check("llArrVal", 40 + i + offset, data->llArrVal[i]);
    }
    for (int i = 0; i < 21; ++i) {
        check("ullArrVal", 41 + i + offset, data->ullArrVal[i]);
    }
    for (int i = 0; i < 22; ++i) {
        check("int64ArrVal", 42 + i + offset, data->int64ArrVal[i]);
    }
    for (int i = 0; i < 23; ++i) {
        check("uint64ArrVal", 43 + i + offset, data->uint64ArrVal[i]);
    }
    // 检查基本数据类型sequence

    for (int i = 0; i < 2; ++i) {
        check("bSeq", (24 + i + offset) % 2, DDS_BooleanSequence_data(&data->bSeq)[i]);
    }
    for (int i = 0; i < 3; ++i) {
        check("cSeq", 'A' + (25 + i + offset) % 26, DDS_CharSequence_data(&data->cSeq)[i]);
    }
    for (int i = 0; i < 4; ++i) {
        check("oSeq", (unsigned char)(26 + i + offset), DDS_UCharSequence_data(&data->oSeq)[i]);
    }
    for (int i = 0; i < 5; ++i) {
        check("int8Seq", (char)(27 + i + offset), DDS_CharSequence_data(&data->int8Seq)[i]);
    }
    for (int i = 0; i < 6; ++i) {
        check("uint8Seq", (unsigned char)(28 + i + offset), DDS_UCharSequence_data(&data->uint8Seq)[i]);
    }
    for (int i = 0; i < 7; ++i) {
        checkWide("wcSeq", (wchar_t)(L'A' + (29 + i + offset) % 26), DDS_WCharSequence_data(&data->wcSeq)[i]);
    }
    for (int i = 0; i < 8; ++i) {
        check("shortSeq", (short)(30 + i + offset), DDS_ShortSequence_data(&data->shortSeq)[i]);
    }
    for (int i = 0; i < 9; ++i) {
        check("ushortSeq", (unsigned short)(31 + i + offset), DDS_UShortSequence_data(&data->ushortSeq)[i]);
    }
    for (int i = 0; i < 10; ++i) {
        check("int16Seq", (short)(32 + i + offset), DDS_ShortSequence_data(&data->int16Seq)[i]);
    }
    for (int i = 0; i < 11; ++i) {
        check("uint16Seq", (unsigned short)(33 + i + offset), DDS_UShortSequence_data(&data->uint16Seq)[i]);
    }
    for (int i = 0; i < 12; ++i) {
        check("longSeq", 34 + i + offset, DDS_LongSequence_data(&data->longSeq)[i]);
    }
    for (int i = 0; i < 13; ++i) {
        check("ulongSeq", 35 + i + offset, DDS_ULongSequence_data(&data->ulongSeq)[i]);
    }
    for (int i = 0; i < 14; ++i) {
        check("int32Seq", 36 + i + offset, DDS_LongSequence_data(&data->int32Seq)[i]);
    }
    for (int i = 0; i < 15; ++i) {
        check("uint32Seq", 37 + i + offset, DDS_ULongSequence_data(&data->uint32Seq)[i]);
    }
    for (int i = 0; i < 16; ++i) {
        check("floatSeq", (38 + i + offset), DDS_FloatSequence_data(&data->floatSeq)[i]);
    }
    for (int i = 0; i < 17; ++i) {
        check("doubleSeq", (39 + i + offset), DDS_DoubleSequence_data(&data->doubleSeq)[i]);
    }
    for (int i = 0; i < 18; ++i) {
        check("ldoubleSeq", (40 + i + offset), DDS_LongDoubleSequence_data(&data->ldoubleSeq)[i].ld);
    }
    for (int i = 0; i < 19; ++i) {
        check("llSeq", 41 + i + offset, DDS_LongLongSequence_data(&data->llSeq)[i]);
    }
    for (int i = 0; i < 20; ++i) {
        check("ullSeq", 42 + i + offset, DDS_ULongLongSequence_data(&data->ullSeq)[i]);
    }
    for (int i = 0; i < 21; ++i) {
        check("int64Seq", 43 + i + offset, DDS_LongLongSequence_data(&data->int64Seq)[i]);
    }
    for (int i = 0; i < 22; ++i) {
        check("uint64Seq", 44 + i + offset, DDS_ULongLongSequence_data(&data->uint64Seq)[i]);
    }
    // 检查map
    for (int i = 0; i < 2; ++i) {
        DDS_Long expectedKey = 47 + i + offset;
        DDS_Short expectedValue = (short)(47 + i + offset);
        DDS_Long* key = NULL;
        DDS_Short* mapValue = NULL;
        if (!DDS_LongDDS_ShortMap_index(&data->lsMap, i, &key, &mapValue)) {
            printf("Error: lsMap does not contain key %ld\n", *key);
            continue;
        }
        check("lsMap", expectedKey, *key);
        check("lsMap", expectedValue, *mapValue);
    }
}

void checkComplexStruct(const test_ComplexStruct* data, int offset) {
    // 继承的父类成员
    char expectedName[64];
    snprintf(expectedName, sizeof(expectedName), "ParentName_%d", 0 + offset);
    if (strcmp(expectedName, DDSCString_c_str(&data->parent.name)) != 0) {
        printf("Error in name: expected %s, got %s\n", expectedName, DDSCString_c_str(&data->parent.name));
    }

    // 检查SimpleStruct成员
    checkSimpleStruct(&data->structMem, 1 + offset);

    // 检查SimpleStruct数组成员
    for (int i = 0; i < 2; ++i) {
        checkSimpleStruct(&data->structArrayMem[i], 2 + i + offset);
    }

    // 检查SimpleStruct序列成员
    for (int i = 0; i < test_SimpleStructSequence_size(&data->structSeqMem); ++i) {
        test_SimpleStruct* curEle = test_SimpleStructSequence_at(&data->structSeqMem, i);
        checkSimpleStruct(curEle, 3 + i + offset);
    }

    // 检查SimpleStruct map成员
    for (int i = 0; i < 3; ++i) {
        char expectedKey[64];
        snprintf(expectedKey, sizeof(expectedKey), "Key_%d", 4 + i + offset);
        DDS_CString* key = DDSCString_from_cstr(expectedKey);
        test_SimpleStruct* value = NULL;
        if (!DDS_CStringtest_SimpleStructMap_index(&data->structMapMem, i, &key, &value)) {
            printf("Error: structMapMem does not contain key %s\n", expectedKey);
            continue;
        }
        checkSimpleStruct(value, 4 + i + offset);
    }
}

void test_cdr_deserialize() {
    char* buffer = NULL;
    unsigned int bufferSize = 0;
    int ret = test_cdr_serialize(&buffer, &bufferSize);
    if (ret == 0) {
        printf("Serialization successful, data written to test_cdr_serialize.txt\n");
    }
    else {
        printf("Serialization failed\n");
        return;
    }
    // 反序列化测试
    test_ComplexStruct* data = test_ComplexStruct_create();
    if (data == NULL) {
        printf("Failed to create test_ComplexStruct instance for deserialization\n");
        return;
    }
    ret = test_ComplexStruct_deserialize(data, buffer, bufferSize);
    if (ret != 0) {
        printf("Deserialization failed\n");
        test_ComplexStruct_destroy(data);
        return;
    }
    // 检查反序列化结果
    checkComplexStruct(data, 0);
}

int main(int argc, char* argv[]) {
    // 序列化测试
    test_cdr_serialize(NULL, NULL);
    // 反序列化测试
    test_cdr_deserialize();
    return 0;
}
